//
//  BDGoumaiJiluViewController.m
//  BadBoy
//
//  Created by wen on 2017/4/26.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDGoumaiJiluViewController.h"
#import "BDGoumaiJiluTableViewCell.h"
#import "BDUserModel.h"

@interface BDGoumaiJiluViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;

@end

@implementation BDGoumaiJiluViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"购买记录");
    
    [self requestDatsource];
    
}

- (void)requestDatsource{
    NSString *url = [NSString stringWithFormat:@"%@user/productPurchase?page=%@&size=%@", KBDBaseURL, self.startPage,self.pageSize];
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        [self.datasource addObjectsFromArray:x[@"list"]];
        self.isEnd = [x[@"is_end"] boolValue];
        self.startPage = x[@"next_page"];
        [self setTableiewFooterViewStateFor:self.tableView datasourceCount:self.datasource.count];
        [self.tableView reloadData];
    }];
    
    [signal subscribeError:^(NSError * _Nullable error) {
        [self showErrorViewWithTapBlock:^{
            
        }];
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BDGoumaiJiluTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goumaiCell" forIndexPath:indexPath];
    [cell configeWithModel:self.datasource[indexPath.row]];
    if (indexPath.row == 0) {
        cell.isFirst = YES;
    }else{
        cell.isFirst = NO;
    }
    return cell;
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight) style:UITableViewStylePlain];
        _tableView.sectionFooterHeight = 0;
        _tableView.sectionHeaderHeight = 0;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
//        _tableView.tableHeaderView = self.headerView;
//        _tableView.tableFooterView = self.footerView;
        [_tableView registerClass:[BDGoumaiJiluTableViewCell class] forCellReuseIdentifier:@"goumaiCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)datasource{
    if (!_datasource) {
        self.datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
